package com.zhangql.frame;

import java.text.DecimalFormat;

import com.zhangql.R;
import com.zhangql.util.ActivityStack;
import com.zhangql.util.DataStore;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class onfootframe extends Activity{

	private Button startButton = null;
	private Button endButton = null;
	private Button recordButton = null;
	private TextView hour = null;
	private TextView min = null;
	private TextView second = null;
	private TextView kaluli = null;
	private boolean paused = false;
	private int timeUsedInSec;
	
	private DataStore dataStore = new DataStore();
	
	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				if (!paused)
				{
					addTimeUsed();
					updateClockUI();
				}
				handler.sendEmptyMessageDelayed(1, 1000);
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.onfoot);
		
		startButton = (Button)findViewById(R.id.startButtuonFoot);
		endButton = (Button)findViewById(R.id.endButtonFoot);
		recordButton = (Button)findViewById(R.id.recordButtonFoot);
		
		hour = (TextView)findViewById(R.id.hourFoot);
		min = (TextView)findViewById(R.id.minFoot);
		second = (TextView)findViewById(R.id.secondFoot);
		
	    kaluli = (TextView)findViewById(R.id.kaluliFoot);
		
		startButton.setOnClickListener(new startButtonlinstenerFoot());
		endButton.setOnClickListener(new endButtonlinstenerFoot());
		recordButton.setOnClickListener(new recordButtonlinstenerFoot());
		
		
		ActivityStack.getInstance().addActivity(this);
	}
	
	private void updateClockUI() {
		min.setText(getMin());
		second.setText(getSec());
		hour.setText(getHour());
		
		kaluli.setText(getKaluli());
	}
	
	public void addTimeUsed() {
		timeUsedInSec=timeUsedInSec+1;
	}
	
	class startButtonlinstenerFoot implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			handler.removeMessages(1);
			startGame();	
			paused = false;		
	    }
		
	}
	
	class endButtonlinstenerFoot implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			paused = true;
			//timeUsedInSec=0;
		}
	}
	
	class recordButtonlinstenerFoot implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(paused){
				String kakuli = onfootframe.this.getKaluli().toString();
				Context ctx = onfootframe.this;
				dataStore.recordData(ctx,timeUsedInSec,kakuli,"sportData","步行");
				timeUsedInSec = 0;
			}else{
				paused = true;
				onClick(v);
			}
		}
	}
	
	
	
	@Override
	protected void onPause() {
		super.onPause();
		paused = true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		paused = false;
	}
	
	private void startGame() {
		handler.sendEmptyMessageDelayed(1, 1000);
	}
	
	public CharSequence getMin() {
		return String.valueOf(timeUsedInSec / 60);
	}
	
	public CharSequence getHour() {
		return String.valueOf(timeUsedInSec / 3600);
	}

	public CharSequence getSec() {
		int sec = timeUsedInSec % 60;
		return sec < 10 ? "0" + sec : String.valueOf(sec);
	}
	
	/**
	 * ͨ�������ļ���ȡ���㿨·�﹫ʽ
	 * @return
	 */
	public String getKaluli(){
		//String xString = new DecimalFormat("###,###,###.##").format(t);
		//return timeUsedInSec*0.075+"";
		return new DecimalFormat("###,###,###.##").format(timeUsedInSec*0.075);
	}
    
	
	
}
